package com.runen.wnhz.runen.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.runen.wnhz.runen.common.login.Wx.WXLoginActivity;
import com.runen.wnhz.runen.common.login.Wx.WeiXinUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private final String TAG = WXEntryActivity.class.getSimpleName();
    private WeiXinUtils mUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        this.mUtils = WeiXinUtils.getInstance();
        Log.e(TAG, "到微信回调这里了");
        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，
        // 需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，
        // 应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            boolean result = mUtils.getWxApi().handleIntent(getIntent(), this);
            if (!result) {
                //参数不合法
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mUtils.getWxApi().handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mUtils.getWxApi().handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.e(TAG, "微信baseReq:" + baseReq.toString());
    }

    @Override
    public void onResp(BaseResp baseResp) {
//        baseresp.getType 1:第三方授权， 2:分享
        Log.e(TAG, "微信baseResp:" + baseResp.errStr + "," + baseResp.openId + "," + baseResp.transaction + "," + baseResp.errCode);
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                //用户同意
                String code = ((SendAuth.Resp) baseResp).code;
                Log.e(TAG, "微信回调Code=" + code);
                if (((SendAuth.Resp) baseResp).state.equals(WXLoginActivity.STATE))
                    WeiXinUtils.getInstance().getWxProxyActivity().onWXEntryActivityResult(code);

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                //用户取消
                WeiXinUtils.getInstance().getWxProxyActivity().onCancel();
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //用户拒绝授权
                WeiXinUtils.getInstance().getWxProxyActivity().onDenied();
                finish();
                break;
            default:
                finish();
                break;
        }

    }
}
